#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <unistd.h>
#include <sys/shm.h>
/*
    shmget
    shmctl
    shmat
    shmdt
*/
#define ERR_EXIT(m)\
do\
{\
    perror(m);\
    exit(EXIT_FAILURE);\
}while(0)

const char* PATHNAME = "./test.cc";
const int PROJ_ID = 1;

int main(){
    int key = ftok(PATHNAME,PROJ_ID);
    if(key == -1)ERR_EXIT("ftok");
    const size_t shm_size = sizeof(int); 
    int shmid = shmget(key,shm_size,IPC_CREAT);
    int* pnum = (int*)shmat(shmid,NULL,0);
    if(pnum == (void*)-1)ERR_EXIT("shmat");


    for(int i = 0;i < 15;i++){
        (*pnum)++;
        printf("pnum :%d\n",*pnum);
        fflush(stdout);
        // sleep(1);
    }
    


    if(shmctl(shmid,IPC_RMID,NULL) == -1)ERR_EXIT("shmctl");
    printf("%d SHM DEL\n",key);
    shmdt(pnum);
    return 0;
}